<?php
/*
 * @fn:环境检测与项目安装-即插即用
 * @author:joinerxing
 * @blog:http://www.niaoge.vip/
 * @qq:1253576512
 * @date:2017/03/16
 * @ver:1.0.0
 */
header( 'Content-Type: text/html; charset=utf-8' );
define('DEBUG', true);
if (DEBUG === true) {
    error_reporting(E_ALL);
    ini_set('display_errors', true);
} else {
    error_reporting(0);
}

$boot = new bootSys();
$boot->nocache_headers();
$boot->run();

class bootSys
{
    public $data = array();
    public $cfgchk = array('ver'=>'PHP的版本','ext'=>'PHP扩展','rwx'=>'目录写入权限','cfg'=>'业务配置信息');

    public function chkEnv($config)
    {
        foreach($config as $key=>$val)
        {
            switch($key){
                case 'ext'://检测php扩展
                    foreach($val as $ext){
                        if( in_array($ext,get_loaded_extensions()) )
                        {
                            $this->data['cc'][$key][$ext] = 'yes';
                        }
                        else
                        {
                            $this->data['cc'][$key][$ext] = 'no';
                            $this->data['chk'] = false;
                        }
                    }
                break;

                case 'ver'://检测php版本
                    if (version_compare(PHP_VERSION, $config['ver']['php']) >= 0)
                    {
                        $this->data['cc'][$key]['php'] = 'yes';
                    }
                    else
                    {
                        $this->data['cc'][$key]['php'] = 'no';
                        $this->data['chk'] = false;
                    }
                break;

                case 'cfg'://检测配置源文件是否存在
                    if(file_exists($config['cfg']['pathsrc']))
                    {
                        $this->data['cc'][$key]['cfgsrc'] = 'yes';
                    }
                    else
                    {
                        $this->data['cc'][$key]['cfgsrc'] = 'no';
                        $this->data['chk'] = false;
                    }
                break;

                case 'rwx'://检测目录是否可以读写
                    foreach($val as $fileName){
                        if(is_dir($fileName) && !file_exists($fileName))
                        {
                            mkdir($fileName);
                        }
                        if(is_writable($fileName))
                        {
                            $this->data['cc'][$key][$fileName] = 'yes';
                        }
                        else
                        {
                            $this->data['cc'][$key][$fileName] = 'no';
                            $this->data['chk'] = false;
                        }
                    }
                break;
            }
        }
    }


    public function mkConfig()
    {

    }

    public function run()
    {
        $config = array();
        $step = isset($_REQUEST['step']) ?  max(0,(int)$_REQUEST['step']) : 1;
        $filecfg = dirname(__FILE__).DIRECTORY_SEPARATOR.'bootconfig.php';
        if(!file_exists($filecfg))
        {
            if($step > 1)
            {
                header("Location:?step=1");
                exit;
            }
        }
        else
        {
            require dirname(__FILE__).DIRECTORY_SEPARATOR.'bootconfig.php';//加载配置文件
            if(isset($config['cfg']['pathdest']) &&  file_exists($config['cfg']['pathdest']))//已安装
            {
                exit($this->getHtml(10));
            }
        }
        switch($step){
            case 2:
                $this->chkEnv($config);
            break;

            case 3:
                if(! isset($config['cfg']) )
                {
                    exit($this->getHtml(5));
                }
            break;

            case 4:
            break;

            case 5:
            break;
        }

        exit($this->getHtml($step));
    }

    public function getHtml($type)
    {
        $sHtml = '';
$commHtml =<<<EOF
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="width=device-width"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="robots" content="noindex,nofollow"/>
<title>BOOT &rsaquo; Setup Configuration File</title>
<style>
.wp-core-ui .button,.wp-core-ui .button-primary,.wp-core-ui .button-secondary{display:inline-block;text-decoration:none;font-size:13px;line-height:26px;height:28px;margin:0;padding:0 10px 1px;cursor:pointer;border-width:1px;border-style:solid;-webkit-appearance:none;-webkit-border-radius:3px;border-radius:3px;white-space:nowrap;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}.wp-core-ui button::-moz-focus-inner,.wp-core-ui input[type=reset]::-moz-focus-inner,.wp-core-ui input[type=button]::-moz-focus-inner,.wp-core-ui input[type=submit]::-moz-focus-inner{border-width:0;border-style:none;padding:0}.wp-core-ui .button-group.button-large .button,.wp-core-ui .button.button-large{height:30px;line-height:28px;padding:0 12px 2px}.wp-core-ui .button-group.button-small .button,.wp-core-ui .button.button-small{height:24px;line-height:22px;padding:0 8px 1px;font-size:11px}.wp-core-ui .button-group.button-hero .button,.wp-core-ui .button.button-hero{font-size:14px;height:46px;line-height:44px;padding:0 36px}.wp-core-ui .button:active,.wp-core-ui .button:focus{outline:0}.wp-core-ui .button.hidden{display:none}.wp-core-ui input[type=reset],.wp-core-ui input[type=reset]:active,.wp-core-ui input[type=reset]:focus,.wp-core-ui input[type=reset]:hover{background:0 0;border:none;-webkit-box-shadow:none;box-shadow:none;padding:0 2px 1px;width:auto}.wp-core-ui .button,.wp-core-ui .button-secondary{color:#555;border-color:#ccc;background:#f7f7f7;-webkit-box-shadow:0 1px 0 #ccc;box-shadow:0 1px 0 #ccc;vertical-align:top}.wp-core-ui p .button{vertical-align:baseline}.wp-core-ui .button-secondary:focus,.wp-core-ui .button-secondary:hover,.wp-core-ui .button.focus,.wp-core-ui .button.hover,.wp-core-ui .button:focus,.wp-core-ui .button:hover{background:#fafafa;border-color:#999;color:#23282d}.wp-core-ui .button-link:focus,.wp-core-ui .button-secondary:focus,.wp-core-ui .button.focus,.wp-core-ui .button:focus{border-color:#5b9dd9;-webkit-box-shadow:0 0 3px rgba(0,115,170,.8);box-shadow:0 0 3px rgba(0,115,170,.8)}.wp-core-ui .button-secondary:active,.wp-core-ui .button.active,.wp-core-ui .button.active:hover,.wp-core-ui .button:active{background:#eee;border-color:#999;-webkit-box-shadow:inset 0 2px 5px -3px rgba(0,0,0,.5);box-shadow:inset 0 2px 5px -3px rgba(0,0,0,.5);-webkit-transform:translateY(1px);-ms-transform:translateY(1px);transform:translateY(1px)}.wp-core-ui .button.active:focus{border-color:#5b9dd9;-webkit-box-shadow:inset 0 2px 5px -3px rgba(0,0,0,.5),0 0 3px rgba(0,115,170,.8);box-shadow:inset 0 2px 5px -3px rgba(0,0,0,.5),0 0 3px rgba(0,115,170,.8)}.wp-core-ui .button-disabled,.wp-core-ui .button-secondary.disabled,.wp-core-ui .button-secondary:disabled,.wp-core-ui .button-secondary[disabled],.wp-core-ui .button.disabled,.wp-core-ui .button:disabled,.wp-core-ui .button[disabled]{color:#a0a5aa!important;border-color:#ddd!important;background:#f7f7f7!important;-webkit-box-shadow:none!important;box-shadow:none!important;text-shadow:0 1px 0 #fff!important;cursor:default;-webkit-transform:none!important;-ms-transform:none!important;transform:none!important}.wp-core-ui .button-link{margin:0;padding:0;-webkit-box-shadow:none;box-shadow:none;border:0;-webkit-border-radius:0;border-radius:0;background:0 0;outline:0;cursor:pointer}.wp-core-ui .button-link:focus{outline:#5b9dd9 solid 1px}.wp-core-ui .button-primary{background:#0085ba;border-color:#0073aa #006799 #006799;-webkit-box-shadow:0 1px 0 #006799;box-shadow:0 1px 0 #006799;color:#fff;text-decoration:none;text-shadow:0 -1px 1px #006799,1px 0 1px #006799,0 1px 1px #006799,-1px 0 1px #006799}.wp-core-ui .button-primary.focus,.wp-core-ui .button-primary.hover,.wp-core-ui .button-primary:focus,.wp-core-ui .button-primary:hover{background:#008ec2;border-color:#006799;color:#fff}.wp-core-ui .button-primary.focus,.wp-core-ui .button-primary:focus{-webkit-box-shadow:0 1px 0 #0073aa,0 0 2px 1px #33b3db;box-shadow:0 1px 0 #0073aa,0 0 2px 1px #33b3db}.wp-core-ui .button-primary.active,.wp-core-ui .button-primary.active:focus,.wp-core-ui .button-primary.active:hover,.wp-core-ui .button-primary:active{background:#0073aa;border-color:#006799;-webkit-box-shadow:inset 0 2px 0 #006799;box-shadow:inset 0 2px 0 #006799;vertical-align:top}.wp-core-ui .button-primary-disabled,.wp-core-ui .button-primary.disabled,.wp-core-ui .button-primary:disabled,.wp-core-ui .button-primary[disabled]{color:#66c6e4!important;background:#008ec2!important;border-color:#007cb2!important;-webkit-box-shadow:none!important;box-shadow:none!important;text-shadow:0 -1px 0 rgba(0,0,0,.1)!important;cursor:default}.wp-core-ui .button.button-primary.button-hero{-webkit-box-shadow:0 2px 0 #006799;box-shadow:0 2px 0 #006799}.wp-core-ui .button.button-primary.button-hero.active,.wp-core-ui .button.button-primary.button-hero.active:focus,.wp-core-ui .button.button-primary.button-hero.active:hover,.wp-core-ui .button.button-primary.button-hero:active{-webkit-box-shadow:inset 0 3px 0 #006799;box-shadow:inset 0 3px 0 #006799}.wp-core-ui .button-group{position:relative;display:inline-block;white-space:nowrap;font-size:0;vertical-align:middle}.wp-core-ui .button-group>.button{display:inline-block;-webkit-border-radius:0;border-radius:0;margin-right:-1px;z-index:10}.wp-core-ui .button-group>.button-primary{z-index:100}.wp-core-ui .button-group>.button:hover{z-index:20}.wp-core-ui .button-group>.button:first-child{-webkit-border-radius:3px 0 0 3px;border-radius:3px 0 0 3px}.wp-core-ui .button-group>.button:last-child{-webkit-border-radius:0 3px 3px 0;border-radius:0 3px 3px 0}.wp-core-ui .button-group>.button:focus{position:relative;z-index:1}@media screen and (max-width:782px){.wp-core-ui .button,.wp-core-ui .button.button-large,.wp-core-ui .button.button-small,a.preview,input#publish,input#save-post{padding:6px 14px;line-height:normal;font-size:14px;vertical-align:middle;height:auto;margin-bottom:4px}#media-upload.wp-core-ui .button{padding:0 10px 1px;height:24px;line-height:22px;font-size:13px}.media-frame.mode-grid .bulk-select .button{margin-bottom:0}.wp-core-ui .save-post-status.button{position:relative;margin:0 14px 0 10px}.wp-core-ui.wp-customizer .button{padding:0 10px 1px;font-size:13px;line-height:26px;height:28px;margin:0;vertical-align:inherit}.interim-login .button.button-large{height:30px;line-height:28px;padding:0 12px 2px}}
a img,abbr,fieldset{border:0}#logo,h1,h2{padding:0 0 7px}#logo a,.form-table th p,h1,h2{font-weight:400}html{background:#f1f1f1;margin:0 20px}body{background:#fff;color:#444;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif;margin:140px auto 25px;padding:20px 20px 10px;max-width:700px;-webkit-font-smoothing:subpixel-antialiased;-webkit-box-shadow:0 1px 3px rgba(0,0,0,.13);box-shadow:0 1px 3px rgba(0,0,0,.13)}a{color:#0073aa}a:active,a:hover{color:#00a0d2}a:focus{color:#124964;-webkit-box-shadow:0 0 0 1px #5b9dd9,0 0 2px 1px rgba(30,140,190,.8);box-shadow:0 0 0 1px #5b9dd9,0 0 2px 1px rgba(30,140,190,.8)}.ie8 a:focus{outline:#5b9dd9 solid 1px}h1,h2{border-bottom:1px solid #ddd;clear:both;color:#666;font-size:24px}h3{font-size:16px}dd,dt,li,p{padding-bottom:2px;font-size:14px;line-height:1.5}.code,code{font-family:Consolas,Monaco,monospace}input,submit,textarea{font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif}dl,ol,ul{padding:5px 5px 5px 22px}abbr{font-variant:normal}fieldset{padding:0;margin:0}label{cursor:pointer}#logo{margin:6px 0 14px;border-bottom:none;text-align:center}#logo a{-webkit-background-size:84px;background-size:84px;background-position:center top;background-repeat:no-repeat;color:#444;height:84px;font-size:20px;line-height:1.3em;margin:-130px auto 25px;padding:0;text-decoration:none;width:84px;text-indent:-9999px;outline:0;overflow:hidden;display:block}#pass1-text,.pw-weak,.show-password #pass1{display:none}#logo a:focus{-webkit-box-shadow:none;box-shadow:none}.step{margin:20px 0 15px}.step,th{text-align:left;padding:0}.language-chooser.wp-core-ui .step .button.button-large{height:36px;font-size:14px;line-height:33px;vertical-align:middle}.form-table td,.form-table th{padding:10px 20px 10px 0;vertical-align:top;font-size:14px}textarea{border:1px solid #ddd;width:100%;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}.form-table{border-collapse:collapse;margin-top:1em;width:100%}.form-table td{margin-bottom:9px}.form-table th{text-align:left;width:140px}.form-table code{line-height:18px;font-size:14px}.form-table p{margin:4px 0 0;font-size:11px}.form-table input{line-height:20px;font-size:15px;padding:3px 5px;border:1px solid #ddd;-webkit-box-shadow:inset 0 1px 2px rgba(0,0,0,.07);box-shadow:inset 0 1px 2px rgba(0,0,0,.07)}.form-table input[type=email],.form-table input[type=password],.form-table input[type=text],.form-table input[type=url]{width:206px}.form-table.install-success td,.form-table.install-success th{vertical-align:middle;padding:16px 20px 16px 0}.form-table.install-success td p{margin:0;font-size:14px}.form-table.install-success td code{margin:0;font-size:18px}#error-page{margin-top:50px}#error-page p{font-size:14px;line-height:18px;margin:25px 0 20px}#error-page code,.code{font-family:Consolas,Monaco,monospace}.wp-hide-pw>.dashicons{line-height:inherit}#pass-strength-result{background-color:#eee;border:1px solid #ddd;color:#23282d;margin:-2px 5px 5px 0;padding:3px 5px;text-align:center;width:218px;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;opacity:0}#pass-strength-result.short{background-color:#f1adad;border-color:#e35b5b;opacity:1}#pass-strength-result.bad{background-color:#fbc5a9;border-color:#f78b53;opacity:1}#pass-strength-result.good{background-color:#ffe399;border-color:#ffc733;opacity:1}#pass-strength-result.strong{background-color:#c1e1b9;border-color:#83c373;opacity:1}#pass1-text.short,#pass1.short{border-color:#e35b5b}#pass1-text.bad,#pass1.bad{border-color:#f78b53}#pass1-text.good,#pass1.good{border-color:#ffc733}#pass1-text.strong,#pass1.strong{border-color:#83c373}.message{border-left:4px solid #dc3232;padding:.7em .6em;background-color:#fbeaea}#admin_email,#dbhost,#dbname,#pass1,#pass2,#prefix,#pwd,#uname,#user_login{direction:ltr}.show-password #pass1-text{display:inline-block}.form-table span.description.important{font-size:12px}.rtl input,.rtl submit,.rtl textarea,body.rtl{font-family:Tahoma,sans-serif}.language-chooser select,:lang(he-il) .rtl input,:lang(he-il) .rtl submit,:lang(he-il) .rtl textarea,:lang(he-il) body.rtl{font-family:Arial,sans-serif}@media only screen and (max-width:799px){body{margin-top:115px}#logo a{margin:-125px auto 30px}}@media screen and (max-width:782px){.form-table{margin-top:0}.form-table td,.form-table th{display:block;width:auto;vertical-align:middle}.form-table th{padding:20px 0 0}.form-table td{padding:5px 0;border:0;margin:0}input,textarea{font-size:16px}.form-table span.description,.form-table td input[type=text],.form-table td input[type=email],.form-table td input[type=url],.form-table td input[type=password],.form-table td select,.form-table td textarea{width:100%;font-size:16px;line-height:1.5;padding:7px 10px;display:block;max-width:none;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}}body.language-chooser{max-width:300px}.language-chooser select{padding:8px;width:100%;display:block;border:1px solid #ddd;background-color:#fff;color:#32373c;font-size:16px;font-weight:400}.language-chooser p{text-align:right}.screen-reader-input,.screen-reader-text{position:absolute;margin:-1px;padding:0;height:1px;width:1px;overflow:hidden;clip:rect(0 0 0 0);border:0}.spinner{-webkit-background-size:20px 20px;background-size:20px 20px;visibility:hidden;opacity:.7;filter:alpha(opacity=70);width:20px;height:20px;margin:2px 5px 0}.step .spinner{display:inline-block;vertical-align:middle;margin-right:15px}.button.hide-if-no-js,.hide-if-no-js{display:none}@media print,(-webkit-min-device-pixel-ratio:1.25),(min-resolution:120dpi){.spinner{}}
</style>
</head>
##COMM##
<script type='text/javascript' src='//code.jquery.com/jquery-1.12.4.min.js'></script>
<script type='text/javascript' src='//code.jquery.com/jquery-migrate-3.0.0.min.js'></script>
</html>
EOF;

        $num = $type+1;
        switch($type)
        {
            case 1:
$sHtml =<<<eof2
<body class="wp-core-ui">
<p><strong>技术支持 ：<a href="http://www.niaoge.vip/" tabindex="99">鸟哥变形记</a></strong></p>
<p><strong>赞助商 ：<a href="http://rixinke.taobao.com/" tabindex="99">日薪客</a></strong></p>
<p>安装前检测：</p>
<ol>
	<li>PHP的版本</li>
	<li>PHP扩展</li>
	<li>目录写入权限</li>
	<li>业务配置信息</li>
</ol>
<p><strong>您要做的只是将数据信息填入配置文件。您也可以在文本编辑器中打开<code>bootconfig-src.php</code>，填入您的信息，并将其另存为<code>bootconfig.php</code>。</strong>
	需要更多帮助？<a href="http://www.niaoge.vip/archives/bootconfig.html">看这里</a>。</p>
<p>绝大多数时候，您的网站开发服务者会给您这些信息。如果您没有这些信息，在继续之前您将需要联系他们。如果您准备好了…</p>

<p class="step"><a href="?step={$num}" class="button button-large">现在就开始！</a></p>
</body>
eof2;
                break;

            case 2:
                $sHtml =<<<eof1
<body class="wp-core-ui">
<p><strong>技术支持 ：<a href="http://www.niaoge.vip/" tabindex="99">鸟哥变形记</a></strong></p>
<p><strong>赞助商 ：<a href="http://rixinke.taobao.com/" tabindex="99">日薪客</a></strong></p>
<p>欢迎使用BOOT，检测结果如下：</p>
##str##
<p>绝大多数时候，您的网站开发服务者会给您这些信息。如果您没有这些信息，在继续之前您将需要联系他们。</p>
eof1;
                $aCfg = $this->cfgchk;
                $ss = '';
                foreach($this->data['cc'] as $k=>$v)
                {
                       if(!isset($aCfg[$k])) continue;
                       $ss .= "<p><strong>$aCfg[$k]</strong></p><ol>";
                       foreach($v as $kt=>$vt)
                       {
                           $ss.="<li>$kt => $vt</li>";
                       }
                       $ss .= "</ol>";
                }
                $sHtml = str_replace('##str##',$ss,$sHtml);
                if( isset($this->data['chk']) &&  ($this->data['chk'] === false) )
                {
                    $sHtml .= "<p class=\"step\">请处理以上异常然后，重新安装</p>";
                }
                else
                {
                    $sHtml .= "<p class=\"step\"><a href=\"?step={$num}\" class=\"button button-large\">恭喜，一切顺利</a></p>";
                }
                $sHtml .= "</body>";
            break;

            case 3:
                $sHtml =<<<eof3

eof3;
            break;

            case 4:
                $sHtml =<<<eof4

eof4;
            break;

            case 5:
$sHtml =<<<eof5
<body class="wp-core-ui">
<p><strong>技术支持 ：<a href="http://www.niaoge.vip/" tabindex="99">鸟哥变形记</a></strong></p>
<p><strong>赞助商 ：<a href="http://rixinke.taobao.com/" tabindex="99">日薪客</a></strong></p>
<p><strong>恭喜，您已安装成功！</strong></p>
<p class="step"><a href="/" class="button button-large">查看首页</a></p>
</body>
eof5;
                break;

            case 10:
                $sHtml =<<<eof10
<body class="wp-core-ui">
<p><strong>技术支持 ：<a href="http://www.niaoge.vip/" tabindex="99">鸟哥变形记</a></strong></p>
<p><strong>赞助商 ：<a href="http://rixinke.taobao.com/" tabindex="99">日薪客</a></strong></p>
<p>您已成功安装，请勿重复安装...</p>
</body>
eof10;
                break;
        }

        $sHtml = str_replace('##COMM##',$sHtml,$commHtml);
        return $sHtml;
    }

    public function nocache_headers()
    {
        $headers = array(
            'Expires' => 'Wed, 11 Jan 1984 05:00:00 GMT',
            'Cache-Control' => 'no-cache, must-revalidate, max-age=0',
        );
        if ( function_exists('apply_filters') )
        {
            $headers = (array) apply_filters( 'nocache_headers', $headers );
        }
        $headers['Last-Modified'] = false;
        unset( $headers['Last-Modified'] );
        // In PHP 5.3+, make sure we are not sending a Last-Modified header.
        if ( function_exists( 'header_remove' ) )
        {
            @header_remove( 'Last-Modified' );
        }
        else
        {
            // In PHP 5.2, send an empty Last-Modified header, but only as a
            // last resort to override a header already sent. #WP23021
            foreach ( headers_list() as $header )
            {
                if ( 0 === stripos( $header, 'Last-Modified' ) )
                {
                    $headers['Last-Modified'] = '';
                    break;
                }
            }
        }
        foreach ( $headers as $name => $field_value )
        {
            @header("{$name}: {$field_value}");
        }
    }
}